package org.ccheng.xztx.service.impl;

import cn.hutool.core.map.MapUtil;
import io.reactivex.Observable;
import org.ccheng.xztx.domain.TaskProgressAward;
import org.ccheng.xztx.domain.TaskProgressAwardInfo;
import org.ccheng.xztx.service.DailyService;
import org.ccheng.xztx.util.Api;
import org.ccheng.xztx.util.ApiConstants;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 日常奖励
 */
@Service
public class DailyServiceImpl implements DailyService {

    /**
     * 获取奖励信息
     *
     * @return
     */
    @Override
    public TaskProgressAwardInfo getTaskProgressAwardInfo() {
        return Api.get(ApiConstants.API_DAILY_PROGRESS_AWARD_INFO, TaskProgressAwardInfo.class);
    }

    /**
     * 一键获取所有奖励
     *
     * @return
     */
    @Override
    public void getAllTheRewards() {
        System.out.println("开始领奖");

        okTakeTaskRes();

        TaskProgressAwardInfo taskProgressAwardInfo = getTaskProgressAwardInfo();
        List<TaskProgressAward> taskProgressAwards = taskProgressAwardInfo.getTaskProgressAwards();

        // 已领奖列表
        List<TaskProgressAward> awards = new ArrayList<>();

        for (TaskProgressAward taskProgressAward : taskProgressAwards) {
            if (taskProgressAward.isAward()) {
                awards.add(taskProgressAward);
                getAllTheRewards(taskProgressAward.getId());
            }
        }

        System.out.println("已领奖：" + awards.size());
        for (TaskProgressAward taskProgressAward : awards) {
            System.out.println(taskProgressAward.getName());
        }
    }

    /**
     * 领奖
     *
     * @param id 奖励编号
     * @return
     */
    @Override
    public String getAllTheRewards(String id) {
        Map<String, Object> params = MapUtil.newHashMap();
        params.put("value", id);
        return Api.get(ApiConstants.API_DAILY_TASK_PROGRESS_AWARD, params);
    }

    public void okTakeTaskRes() {
        System.out.println("一键领取日常");
        Api.get(ApiConstants.API_DAILY_TASK_OK_TAKE_TASK);
    }

}
